<template>
    <Modal v-model="showModal" :mask-closable="false" @on-ok="okPassword" @on-cancel="cancel('pwdModal')" 
        :loading="loading" title="重置密码" width="400">
        <Form ref="pwdModal" :model="pwdModal" :rules="ruleInline" label-position="left" :label-width="140">
           <Row>
                <Col span="25">
                    <FormItem label="请输入原密码:" prop="old_password">
                        <Input v-model="pwdModal.old_password" type="password" password/>
                    </FormItem>
                </Col>
            </Row>
            <Row>
                <Col>
                    <span style="color: rgba(0, 0, 0, 0.45); font-size: 13px;">密码最小长度为8,至少各包含一位大小写字母和特殊字符!</span>
                </Col>
            </Row>
            <Row>
                <Col span="25">
                    <FormItem label="请输入新密码:" prop="password">
                        <Input v-model="pwdModal.password" type="password" password/>
                    </FormItem>
                </Col>
            </Row>
            <Row>
                <Col span="25">
                    <FormItem label="请再次输入新密码:" prop="password02">
                        <Input v-model="pwdModal.password02" type="password" password />
                    </FormItem>
                </Col>
            </Row>
        </Form>
    </Modal>
</template>
<script>
    import ajax from "../ajax_util"
    import md5 from 'js-md5'
    import * as types from '../store/types'
    import rsa from '../utils/rsa'

    export default {
        components: {rsa},
        data(){
            // const validatePassword= (rule, value, callback) => {
            //     this.ifRight=-1;
            //     let reg = /^[a-zA-Z][`~!@#$%^&*()_\-+=<>?:"{}|,.\/;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、\w]{5,17}$/;
            //     if (value === '') {
            //         callback(new Error('请输入账号新密码!'));
            //     } else {
            //         if(!reg.test(value)){
            //             this.ifRight=1;
            //             callback(new Error('密码格式不规范!'));
            //         }
            //         callback();
            //     }
            // };
            return{
                ruleInline: {
                    old_password: [
                        { required: true, message: "请输入原密码", trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: "请输入新密码", trigger: 'blur' }
                    ],
                    password02: [
                        { required: true, message: "请输入新密码", trigger: 'blur' }
                    ],
                },
                loading: true,
                pwdModal:{
                    id: this.userId,
                    show: true,
                    old_password: "",
                    password: "",
                    password02:"",
                },
                ifRight:-1,
            }
        },
        methods:{
            cancel(name) {
                this.$refs[name].resetFields();
                this.ifRight = -1;
                this.showModal = false
            },
            okPassword() {
                console.log("======ss======,",this.ifRight);
                if(this.pwdModal.old_password === "" || this.pwdModal.password === "" || this.pwdModal.password02 === ""){
                    this.messageWarningFn('请输入密码!');
                    return
                }else if(this.pwdModal.password != this.pwdModal.password02){
                    this.messageWarningFn('两次新密码不一致!');
                    return
                }else{
                    var that=this;
                    var url=this.CONFIG.url_know+'/person-pwd';
                    let user={
                      user_number: this.$store.state.user.user_number,
                      password:rsa.cmdRSAEncrypt(this.pwdModal.password),
                      old_password:rsa.cmdRSAEncrypt(this.pwdModal.old_password)
                    };
                    console.log("--uuu--",user);
                    ajax.post({
                        url:url,
                        that:that,
                        data:user,
                        success:function(data){
                            if(data == "200"){
                                that.$Notice.success({
                                    title: '成功重置密码,请重新登录！',
                                    duration: 3,
                                    onClose: function(){
                                        that.logout()
                                    }
                                });
                                that.showModal = false
                            }else{
                                that.$Notice.error({
                                    title: '修改密码失败',
                                    desc: data
                                });
                                setTimeout(() => {
                                    that.loading = false
                                    that.$nextTick(() => {
                                      that.loading = true
                                    });
                                }, 1);
                            }
                        }
                    })
                }
            },
            logout(){
                this.$store.commit(types.TITLE, 'logout')
                var that=this;
                var url=this.CONFIG.url_know+'/logout';
                ajax.post({
                    url:url,
                    that:that,
                    success:function(data){
                        if(data.code === 200){
                            sessionStorage.clear();
                            that.$store.commit(types.LOGOUT);
                            that.$router.push({ path:'/login'});
                            that.$Notice.success({
                                title: '已退出，请重新登录!'
                            });
                        }else{
                            that.$Message.error('系统错误!');
                        }
                    }
                })
            },
            messageWarningFn (text) {
              this.$Message.warning(text)
              setTimeout(() => {
                this.loading = false
                this.$nextTick(() => {
                  this.loading = true
                });
              }, 1);
            },
        },
        props:{
            value: {},
            userId:{},
            isLoginRest: {
                default: false
            },
        },
        computed:{
            showModal: {
                get:function() {
                    return this.value; 
                },
                set:function(value) {
                    this.$emit('input', value);
                }
            },
        },
    }
</script>